import { defineStore } from 'pinia'
import { BettingSelection, FeedType } from '@/types'
import { getItemById } from '@/utils'
import { useMatchStore } from './match'

export const useFeedStore = defineStore('feed', {
	state: () => ({
		feedData: {} as Record<string, FeedType[]>,
		descriptionOrderNo: '',
		commentFeed: null as FeedType | null
	}),
	actions: {
		setList(list: FeedType[], type: string = 'feed') {
			// const memberStore = useMemberStore()
			const matchStore = useMatchStore()
			this.feedData[type] = list
			const userIds: string[] = []
			const eventIds: string[] = []
			list.forEach((item: FeedType) => {
				if (!userIds.includes(item.userId)) {
					userIds.push(item.userId)
				}
				item.selections.forEach((selection: BettingSelection) => {
					if (!eventIds.includes(selection.eventId)) {
						eventIds.push(selection.eventId)
					}
				})
			})
			// memberStore.getUserTags(userIds)
			matchStore.setIdList(eventIds, type)
		},
		showDescription(val: string = '') {
			this.descriptionOrderNo = val
		},
		showComment(val: FeedType | null) {
			this.commentFeed = val
		},
		setDescription(orderNo: string, desc: string) {
			Object.values(this.feedData).forEach((list: FeedType[]) => {
				const item = getItemById<FeedType>(list, orderNo, 'orderNo')
				item && (item.content = desc)
			})
		},
		addComment(id: number) {
			Object.values(this.feedData).forEach((list: FeedType[]) => {
				const item = getItemById<FeedType>(list, id)
				item && item.commentNum++
			})
		},
		addFollow(id: number) {
			Object.values(this.feedData).forEach((list: FeedType[]) => {
				const item = getItemById<FeedType>(list, id)
				item && item.followNum++
			})
		}
	},
	getters: {}
	// persist: true
})
